<template>
  <div class="components-container">
    <div class="header-form">
      <div class="layui-inline">
        <label>统计日期</label>
        <div class="layui-input-inline">
          <el-date-picker v-date v-model="selectData.CHDDATEYEARMONTH" type="month" placeholder="请选择日期" style="width:160px;" />
        </div>
      </div>
      <el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading" style="margin-left:20px;">搜索</el-button>
      <el-button @click="btnRepeat">重置</el-button>
    </div>
    
    <!-- 功能列表 -->
    <div style="margin-top:10px;">
      <ul class="toolList">
        <li><el-button type="primary" @click="btnSearch" :loading="this.$store.state.isloading">刷新</el-button></li>
        <li><el-button type="primary" @click="exportExcel">导出Excel</el-button></li>
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon" @click="operate(item.BTNVALUE)">
          <el-button type="primary" :value="item.BTNSHOWNAME" v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <!-- 数据列表 -->
    <dataTable :headList="headList" :dataList="dataList" :hiddenColumn="hiddenColumn" :hiddenPageOrder="false" :pageOrder="pageOrder" @pageChange="pageChange" @selectRow="selectRow"></dataTable>
    <h2 style="text-align:center;background:">{{message}}</h2>
    <printmodal ref="printPdf"></printmodal>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";
  .el-button+.el-button {
    margin-left: 0;
  }
  .btnCenter {
    text-align: center;
  }
</style>
<script>
  import dataTable from "@/components/Table/DataTable";
  import printmodal from "@/components/Pdf/Print";
  import apiBase from "@/api/apiBase.js";
  import { ORDER } from "@/const/orderConst.js";
  import {
    formatDate,
    timeStringToDate
  } from "@/utils/date.js";
  import {
    manage
  } from "@/mixins/managePage.js";
  const INFO = "详情";
  export default {
    name: "assUse",
    mixins: [manage],
    data() {
      return {
        pageWidth: 1200,
        selectData: {
          CHDDATEYEARMONTH: null,
        },
        printUrl: null,
        title: null,
        hiddenColumn:false,
        message: null,
        headList:[],
      };
    },
    methods: {
      exportExcel() {
            var header
            var startTime
            if(this.selectData.CHDDATEYEARMONTH){
                startTime = this.selectData.CHDDATEYEARMONTH.toLocaleDateString();
            }else{
                startTime = '空';
            }
                header = localStorage.getItem('orgname') + '收入明细表' + '(' + startTime + '--' + '空' + ')';
            if (this.dataList.length === 0){
                this.$message.error("无导出数据！");
                return;
            }
            this.downloadLoading = true;
            
            require.ensure([], () => {
                const { export_json_to_excel } = require("@/vendor/Export2Excel");
                export_json_to_excel(this.headList, this.dataList, header);
                this.downloadLoading = false;
            });
        },
      //  功能操作
      operate(order) {
        this.order = order;
        switch (this.order) {
          case '导出Excel':
            this.exportExcel()
            break;
          case '打印':
            this.$refs.printPdf.report(
              '打印收入明细表',
              this.getDataC()
            );
            break;
        }
      },
      //获取表头
      getCreColumn() {
        let data = {};
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Column.GetincomeDetailReportColums,
          res => {
            this.headList = res.data.content;
          });

      },
      //获取查询数据
      getData() {
        this.dataList = [];
        let Data = JSON.parse(JSON.stringify(this.selectData));
        if (Data.CHDDATEYEARMONTH)
          Data.CHDDATEYEARMONTH = formatDate(Data.CHDDATEYEARMONTH, "yyyyMM");
        let data = {
          PageIndex: -1,
          PageSize: 0,
          OrderBy: this.pageOrder.orderType,
          OrderByType: this.pageOrder.orderSort,
          Data: Data
        };
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Report.QueryIncomeDetailReport,
          res => {
            this.dataList = res.data.content;
            this.message = res.data.content.Message;
          });
      },
      getDataC() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      if (Data.CHDDATEYEARMONTH)
        Data.CHDDATEYEARMONTH = formatDate(Data.CHDDATEYEARMONTH, "yyyyMM");
      let data = {
        PageIndex: -1,
        PageSize: 0,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      return data;
    },
      //重置查询
      btnRepeat() {
        this.selectData = {
          CHDDATEYEARMONTH: null,
        };
        this.selectData.CHDDATEYEARMONTH = new Date();
        this.selectData.CHDDATEYEARMONTH.setDate(1);
        this.getData();
      },
       // 控制是否显示
      btnShow(val){
        switch (val) {
          case '导出Excel':
          case '打印':
            return true
            break;
        }
      },
    },
    components: {
      dataTable,
      printmodal,
    },
    created() {
      this.selectData.CHDDATEYEARMONTH = new Date();
      this.selectData.CHDDATEYEARMONTH.setDate(1);
      this.getCreColumn();
      this.getData();
    }
  };
</script>
